package org.jeecg.modules.questions;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.questions.entity.HistoryInfo;
import org.jeecg.common.modules.questions.entity.QuestionHistory;
import org.jeecg.common.modules.questions.service.IHistoryInfoService;
import org.jeecg.common.modules.questions.service.IQuestionHistoryService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 历史主表
 * @Author: jeecg-boot
 * @Date:   2022-02-10
 * @Version: V1.0
 */
@Api(tags="历史主表")
@RestController
@RequestMapping("/questionHistory/questionHistory")
@Slf4j
public class QuestionHistoryController extends JeecgController<QuestionHistory, IQuestionHistoryService> {
	@Autowired
	private IQuestionHistoryService questionHistoryService;
	@Autowired
	private IHistoryInfoService historyInfoService;
	/**
	 * 分页列表查询
	 *
	 * @param questionHistory
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "历史主表-分页列表查询")
	@ApiOperation(value="历史主表-分页列表查询", notes="历史主表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(QuestionHistory questionHistory,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<QuestionHistory> queryWrapper = QueryGenerator.initQueryWrapper(questionHistory, req.getParameterMap());
		Page<QuestionHistory> page = new Page<QuestionHistory>(pageNo, pageSize);
		IPage<QuestionHistory> pageList = questionHistoryService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param questionHistory
	 * @return
	 */
	@AutoLog(value = "历史主表-添加")
	@ApiOperation(value="历史主表-添加", notes="历史主表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody QuestionHistory questionHistory) {
		questionHistoryService.save(questionHistory);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param questionHistory
	 * @return
	 */
	@AutoLog(value = "历史主表-编辑")
	@ApiOperation(value="历史主表-编辑", notes="历史主表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody QuestionHistory questionHistory) {
		questionHistoryService.updateById(questionHistory);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "历史主表-通过id删除")
	@ApiOperation(value="历史主表-通过id删除", notes="历史主表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		questionHistoryService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "历史主表-批量删除")
	@ApiOperation(value="历史主表-批量删除", notes="历史主表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.questionHistoryService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "历史主表-通过id查询")
	@ApiOperation(value="历史主表-通过id查询", notes="历史主表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		QuestionHistory questionHistory = questionHistoryService.getById(id);
		if(questionHistory==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(questionHistory);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param questionHistory
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, QuestionHistory questionHistory) {
        return super.exportXls(request, questionHistory, QuestionHistory.class, "历史主表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, QuestionHistory.class);
    }
	/**
	 * 获取历史回答问卷
	 *
	 * @param
	 * @return
	 */
	@AutoLog(value = "历史回答问卷")
	@ApiOperation(value = "历史回答问卷", notes = "历史回答问卷")
	@PostMapping(value = "/history")
	public Result<?> getHistory(QuestionHistory questionHistory){
		String id = questionHistory.getId();
		HistoryInfoVO historyInfoVO = new HistoryInfoVO();
		List<QuestionVO> questionVOS = new ArrayList<>();
		QuestionVO questionVO = new QuestionVO();
		//查询历史详情
		QuestionHistory historty = questionHistoryService.getById(id);
		BeanUtils.copyProperties(historty,historyInfoVO);
		List<HistoryInfo> info = historyInfoService.list(Wrappers.lambdaQuery(HistoryInfo.class).eq(HistoryInfo::getHistoryId,id).orderByAsc(HistoryInfo::getNumber));
		historyInfoVO.setHistoryInfoList(info);
		return Result.ok(historyInfoVO);
	}
}
